<?php
session_start();
// Now include the Google API client library for PHP(https://code.google.com/p/google-api-php-client/downloads/list)
require_once 'src/Google_Client.php'; // include the required calss files for google login
require_once 'src/contrib/Google_PlusService.php';
require_once 'src/contrib/Google_Oauth2Service.php';
require '../_admin/include/sql.php';
$ini = parse_ini_file ( "_admin/config/config.ini" );
// Include google API settings
$clientID 		= $ini["clientID"];
$clientSecret 	= $ini["clientSecret "];
$redirectURL 	= $ini["redirectURL"];
$apiKey 		= $ini["apiKey"];

$client = new Google_Client();
$client->setApplicationName("DT24.vn"); // Set your applicatio name
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/plus.me')); // set scope during user login
$client->setClientId($clientID); // paste the client id which you get from google API Console
$client->setClientSecret($clientSecret); // set the client secret
$client->setRedirectUri($redirectURL); // paste the redirect URI where you given in APi Console. You will get the Access Token here during login success
$client->setDeveloperKey($apiKey); // Developer key
$plus       = new Google_PlusService($client);
$oauth2     = new Google_Oauth2Service($client); // Call the OAuth2 class for get email address

if (isset($_GET['code']))
{
$client->authenticate(); // Authenticate
    $_SESSION['access_token'] = $client->getAccessToken(); // get the access token here
    header('Location: '.$redirectURL);
}
if(isset($_SESSION['access_token'])) {
	$client->setAccessToken($_SESSION['access_token']);
}


if ($client->getAccessToken()) {
  $user         = $oauth2->userinfo->get();
  $me           = $plus->people->get('me');
  $optParams    = array('maxResults' => 100);
  $activities   = $plus->activities->listActivities('me', 'public',$optParams);
  // The access token may have been updated lazily.
  $_SESSION['access_token']         = $client->getAccessToken();
  $email                            = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
} else {
    $authUrl = $client->createAuthUrl();
    echo "<a href='$authUrl' class='googleplus_btn'></a>";
}
if(isset($me)){
	//$_SESSION['gplusuer'] = $me; // start the session
	$name1 = $me['displayName'];
	$id1 = $me['id'];
	$email1 = $user['email'];
	processLogin($id1,$name1,$email1,1);
}

// if(isset($_GET['logout'])) {
//   unset($_SESSION['access_token']);
//   unset($_SESSION['gplusuer']);
//   session_destroy();
//   header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); // it will simply destroy the current seesion which you started before
// }

?>

